// shouchang/shouchang/index.js
const app = getApp()

Page({
    data: {
        // 接收传过来的题型id
        tixing: '',
        question_list: [],
        question_num: 0,
        radio: {},
        touchx: 0,
        touchy: 0,
        click: false,
        show: false,
        error_question: [],
        correct_question: [],
        collection: []
      },
   




      onLoad(){
        
          let userid = wx.getStorageSync('userID')
          let index = 0
          this.setData({
              question_num:index
          })
        
        //   通过用户id获取用户收藏题目列表
        wx.request({
        url: app.globalData.baseUrl+`/record/selectCollectid/${userid}`,
        success:(res)=>{
            console.log(res.data);
            this.setData({
              
                collection:res.data
            })
        },
        // 通过题型id获取题目列表
        })
        wx.request({
            url: app.globalData.baseUrl+`/record/selectCollect/${userid}`,
            success:(res)=>{
                // console.log(this.data.collection);
                this.setData({
                    question_list: res.data
                })

                var newData = []
                this.data.question_list.forEach(element =>{
                    //  console.log(this.data.collection);
                    // console.log(element);
                    if(this.data.collection.indexOf(element.questionid) != -1){
                        element.collected = true
                    }
                    else {
                        element.collected = false
                    }

                    newData.push(element)
                })
                // console.log(newData);
                    this.setData({
                      
                        question_list: newData
                    })
                  
            }
        })
      },

         // 点击收藏
         collection(e){
            // console.log(this.data.question_list[this.data.question_num]);
            // console.log(this.data.question_list);
            // console.log(this.data.question_num);
            // console.log(this.data.collection);
            // console.log(this.data.collection.indexOf(this.data.question_list[this.data.question_num].questionid) == -1);
            // 判断题目列表有没有
            if(this.data.collection.indexOf(this.data.question_list[this.data.question_num].questionid) == -1){
                // console.log(this.data.collection.push(this.data.question_list[this.data.question_num].questionid));
                // console.log(this.data.collection);
                this.data.collection.push(this.data.question_list[this.data.question_num].questionid)
      this.data.question_list[this.data.question_num].collected = true;
                let question_list = this.data.question_list
                // console.log(question_list);
                this.setData({
                    question_list
                })
                console.log("收藏成功");
            }

            else{
                 // 取消题目收藏
                this.data.question_list[this.data.question_num].collected = false;
                let question_list = this.data.question_list;
                this.setData({
                  question_list
                })
                let index = this.data.collection.indexOf(this.data.question_list[this.data.question_num].questionid)
                // console.log(index);
                let collection = this.data.collection
                // console.log(collection);
                collection.splice(index,1)
                // console.log(collection.splice(index,1));
                this.setData({
                    collection
                  })

                   // 对接取消题目收藏接口
                let question_id = this.data.question_list[this.data.question_num].questionid
                let userid = wx.getStorageSync('userID')
                wx.request({
                    url: app.globalData.baseUrl+`/record/cancel/${userid}/${question_id}`,
                    method: 'GET',
                    success: (res) => {
                    // console.log(res);
                    }
                })
            }
        },


          // 
         // 获取开始触摸屏幕参数
         touchStart(e) {
            // console.log(e)
            this.setData({
            touchx: e.changedTouches[0].clientX,
            touchy: e.changedTouches[0].clientY
            })
        },

        // 获取停止触摸屏幕参数
        touchEnd(e) {
            // console.log(e);
            let x = e.changedTouches[0].clientX;
            let y = e.changedTouches[0].clientY;
            this.getTouchData(x, y, this.data.touchx, this.data.touchy)
        },

        // 左划切换上一题
        prevquestion() {
            let n = --this.data.question_num
            this.setData({
            question_num: n
            })
            // console.log(this.data.radio[this.data.question_num]);
            if (this.data.radio[this.data.question_num]) {
            // console.log(111);
            this.setData({
                click: true
            })
            } else {
            this.setData({
                click: false
            })
            }
        },

         // 右划切换下一题
        nextquestion() {
            if (this.data.question_num >= 0 && this.data.question_num < this.data.question_list.length - 1) {
            let n = ++this.data.question_num
            this.setData({
                question_num: n
            })
            if (this.data.radio[this.data.question_num]) {
                // console.log(111);
                this.setData({
                click: true
                })
            } else {
                this.setData({
                click: false
                })
            }
            }
        },


        /***
   * 判断用户滑动
   * 左滑还是右滑
   */
        getTouchData(endX, endY, startX, startY) {
            if (endX - startX > 50 && Math.abs(endY - startY) < 50 && this.data.question_num > 0) { //左滑
            this.prevquestion()
            } else if (endX - startX < -50 && Math.abs(endY - startY) < 50 && this.data.question_num < this.data.question_list.length - 1) { //右滑
            this.nextquestion()
            }
        },



     //点击单选框事件
     onChange(event){
        //  console.log(event);
        // 获取data的选项
        let radioarr = this.data.radio
        radioarr[this.data.question_num] = event.detail
        // console.log(radioarr[this.data.question_num]);
        this.setData({
            radio: radioarr
          });
        //   console.log(this.data.question_num < this.data.question_list.length && event.detail.trim() === this.data.question_list[this.data.question_num].answer.trim());
        if(this.data.question_num < this.data.question_list.length && event.detail.trim() === this.data.question_list[this.data.question_num].answer.trim()){
            if(!this.data.click){
                //  console.log(this.data.question_list[this.data.question_num].id);
                 this.data.correct_question.push(this.data.question_list[this.data.question_num].questionid)
            }
            wx.showToast({
                title: '答案正确',
                icon: 'succes',
                duration: 300,
                mask: true
              })
              setTimeout(() => {
                this.setData({
                  click: false
                })
                this.nextquestion()
              }, 300);
        }
        else {
            if (!this.data.click) {
            //   console.log(this.data.question_list[this.data.question_num].id);
              this.data.error_question.push(this.data.question_list[this.data.question_num].questionid)
            }
            wx.showToast({
              title: '答案错误',
              icon: 'error',
              duration: 400,
              mask: true
            })
          }
          this.setData({
            click: true
          })
     } ,


       //  答题卡显示
    chose(){
        this.showPopup()
    },
    // 显示
    showPopup(){
        this.setData({
            show:true
        })
    },
     // 控制弹出层隐藏函数
    onClose() {
        this.setData({
        show: false
        });
    },
    // 点击题目列表的题目索引跳转题目
    changeNum(e) {
        // console.log(e.currentTarget.dataset.index);
        let index = e.currentTarget.dataset.index
        this.setData({
        question_num: index
        })
        this.onClose()
    },

    onUnload(){
        let id = wx.getStorageSync('userID')

        // 增加题目收藏
        wx.request({
            url: app.globalData.baseUrl+'/record/collection',
            method: 'POST',
            data: {
            collectid: this.data.collection,
            userid: id
            },
            success(res) {
            // console.log(res)
            }
        })

    }

})